package GreedyAlgorithm;

import java.util.Arrays;
import java.util.Comparator;

public class Test01 {

    public int maximumUnits(int[][] boxTypes, int truckSize) {
        if (boxTypes.length > 1){
            Arrays.sort(boxTypes, new Comparator<int[]>() {
                @Override
                public int compare(int[] o1, int[] o2) {
                    return Integer.compare(o1[1], o2[1]);
                }
            });// 有小到大排序
        }

        System.out.println(Arrays.deepToString(boxTypes));

        int answer = 0;

        for (int r = boxTypes.length-1; r>=0 && truckSize>0; r--){
            // 求和
            if (boxTypes[r][0] > truckSize){
                System.out.println("yes");
                return answer + truckSize * boxTypes[r][1];// 计算数值 返回
            }
            else {
                answer += boxTypes[r][0] * boxTypes[r][1];
                truckSize -= boxTypes[r][0];
            }
        }
        return answer;
    }

    public static void main(String[] args) {
        Test01 test01 = new Test01();
        System.out.println("test01.maximumUnits(new int[][]{{1,3},{2,2},{3,1}},4) = " + test01.maximumUnits(new int[][]{{1, 3}, {2, 2}, {3, 1}}, 4));
    }
}
